﻿var roleListTableVue = new Vue({
    el: "#roleListTable",
    data: {
        hasNewRole: false,      //判断是否正在新建角色
        hasEditRole: false,     //判断是否正在编辑角色
        roleList: [],
        isDisableDictionary: {}
    },
    methods: {
        AddRole: function () {
            AddRoleDetail(this);
        },
        AddSubRole: function (currentRole) {
            AddSubRoleDetail(this, currentRole);
        },
        SaveRole: function (currentRoleInfo) {
            SaveRoleDetail(this, currentRoleInfo);
        },
        CancelSaveRole: function (currentRoleInfo) {
            CancelSaveRoleDetail(this, currentRoleInfo);
        },
        EditRole: function (currentRoleInfo) {
            EditRoleDetail(this, currentRoleInfo);
        },
        UpdateRole: function (currentRoleInfo) {
            UpdateRoleDetail(this, currentRoleInfo);
        },
        CancelUpdateRole: function (currentRoleInfo) {
            CancelUpdateRoleDetail(this, currentRoleInfo);
        },
        DeleteRole: function (currentRoleInfo) {
            DeleteRoleDetail(this, currentRoleInfo);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.isDisableDictionary = GlobalDictionary.GetTrueOrFalseDictionary();
        ListRole(this);
    }
});

/**
 * 查询所有角色信息
 */
function ListRole(vue) {
    ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Account/ListRole?v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                vue.roleList = res.data;

                //禁用所有Input和Select标签
                vue.$nextTick(function () {
                    $("input").prop("disabled", true);
                    $("select").prop("disabled", true);
                });
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            HideAzLoading();
        }
    })
}

/**
 * 创建指定ParentID的空角色
 */
function CreateRole(parentID) {
    var role = {
        ID: 0,
        Name: "",
        PageName: "",
        PagePath: "",
        Icon: "",
        ParentID: parentID,
        Priority: 0,
        IsDisable: 0
    };
    return role;
}

/**
 * 增加角色
 */
function AddRoleDetail(vue) {
    if (vue.hasEditRole || vue.hasNewRole) {
        toastr.error("请保存正在编辑的角色后再新增角色Please save the role being edited before adding a new role。");
    } else {
        vue.roleList.push({ BaseInfo: CreateRole(0) });
        vue.hasNewRole = true;
    }
}

/**
 * 增加子角色
 */
function AddSubRoleDetail(vue, role) {
    if (vue.hasEditRole || vue.hasNewRole) {
        toastr.error("请保存正在编辑的角色后再新增角色Please save the role being edited before adding a new role。");
    } else {
        if (!role.SubRoles)
            role.SubRoles = [];

        role.SubRoles.push(CreateRole(role.BaseInfo.ID));
        vue.hasNewRole = true;
    }
}

/**
 * 保存新增角色
 */
function SaveRoleDetail(vue, roleInfo) {
    if (CheckRole(roleInfo)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Account/SaveRole",
            data: { role: roleInfo },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存角色信息成功Role information saved successfully。");
                    vue.hasNewRole = false;
                    ListRole(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 取消新增角色
 */
function CancelSaveRoleDetail(vue, roleInfo) {
    ListRole(vue);
    vue.hasNewRole = false;
}

/**
 * 编辑角色
 */
function EditRoleDetail(vue, roleInfo) {
    if (vue.hasNewRole || vue.hasEditRole) {
        toastr.error("请保存正在编辑的角色后再新增角色Please save the role being edited before adding a new role。");
    } else {
        ShowEditRoleButton(vue, roleInfo.ID);
    }
    
}

/**
 * 更新编辑中角色
 */
function UpdateRoleDetail(vue, roleInfo) {
    if (CheckRole(roleInfo)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Account/UpdateRole",
            data: { role: roleInfo },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("更新角色信息成功Role information updated successfully。");
                    HideEditRoleButton(vue, roleInfo.ID);
                    ListRole(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 取消编辑中角色
 */
function CancelUpdateRoleDetail(vue, roleInfo) {
    HideEditRoleButton(vue, roleInfo.ID);   
}

/**
 * 删除编辑中角色
 */
function DeleteRoleDetail(vue, roleInfo) {
    var message = "确认删除角色Confirm deletion of role[" + roleInfo.Name + "]?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Account/DeleteRole",
            data: { RoleID: roleInfo.ID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除角色信息成功Role information deleted successfully。");
                    HideEditRoleButton(vue, roleInfo.ID);
                    ListRole(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 显示编辑角色按钮
 */
function ShowEditRoleButton(vue, roleID) {
    $("#inputArea" + roleID).find("input").prop("disabled", false);
    $("#btnEdit" + roleID).hide();
    $("#btnUpdate" + roleID).show();
    $("#btnCancelUpdate" + roleID).show();
    $("#btnDelete" + roleID).show();
    vue.hasEditRole = true;
}

/**
 * 隐藏编辑角色按钮
 */
function HideEditRoleButton(vue, roleID) {
    $("#inputArea" + roleID).find("input").prop("disabled", true);
    $("#btnEdit" + roleID).show();
    $("#btnUpdate" + roleID).hide();
    $("#btnCancelUpdate" + roleID).hide();
    $("#btnDelete" + roleID).hide();
    vue.hasEditRole = false;
}

/**
 * 检查角色内容正确性
 */
function CheckRole(roleInfo) {
    if (!CheckRoleName(roleInfo))
        return false;
    if (!CheckPriority(roleInfo))
        return false;
    return true;
}

/**
 * 检查角色名字
 */
function CheckRoleName(roleInfo) {
    $("#inputName" + roleInfo.ID).addClass("has-error");
    if (roleInfo.Name != null && roleInfo.Name.length > 0) {
        $("#inputName" + roleInfo.ID).removeClass("has-error");
        return true;
    }
    return false;
}

/**
 * 检查优先级内容
 */
function CheckPriority(roleInfo) {
    var reg = /^[0-9]+$/;
    var result = reg.test(roleInfo.Priority);

    $("#inputPriority" + roleInfo.ID).addClass("has-error");
    if (result) {
        $("#inputPriority" + roleInfo.ID).removeClass("has-error");
        return true;
    }
    return false;
}
